package com.cykj.web.controller.posbiz;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cykj.common.annotation.Log;
import com.cykj.common.core.controller.BaseController;
import com.cykj.common.core.domain.AjaxResult;
import com.cykj.common.enums.BusinessType;
import com.cykj.pos.domain.BizNews;
import com.cykj.pos.service.IBizNewsService;
import com.cykj.common.utils.poi.ExcelUtil;
import com.cykj.common.core.page.TableDataInfo;

/**
 * 新闻Controller
 *
 * @author weijianbo
 * @date 2021-04-26
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/news/news" )
public class BizNewsController extends BaseController {

    private final IBizNewsService iBizNewsService;

    /**
     * 查询新闻列表
     */
    @PreAuthorize("@ss.hasPermi('pos:news:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizNews bizNews) {
        startPage();
        List<BizNews> list = iBizNewsService.queryList(bizNews);
        return getDataTable(list);
    }

    /**
     * 导出新闻列表
     */
    @PreAuthorize("@ss.hasPermi('pos:news:export')" )
    @Log(title = "新闻" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(BizNews bizNews) {
        List<BizNews> list = iBizNewsService.queryList(bizNews);
        ExcelUtil<BizNews> util = new ExcelUtil<BizNews>(BizNews.class);
        return util.exportExcel(list, "news" );
    }

    /**
     * 获取新闻详细信息
     */
    @PreAuthorize("@ss.hasPermi('pos:news:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iBizNewsService.getById(id));
    }

    /**
     * 新增新闻
     */
    @PreAuthorize("@ss.hasPermi('pos:news:add')" )
    @Log(title = "新闻" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizNews bizNews) {
        return toAjax(iBizNewsService.save(bizNews) ? 1 : 0);
    }

    /**
     * 修改新闻
     */
    @PreAuthorize("@ss.hasPermi('pos:news:edit')" )
    @Log(title = "新闻" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizNews bizNews) {
        return toAjax(iBizNewsService.updateById(bizNews) ? 1 : 0);
    }

    /**
     * 删除新闻
     */
    @PreAuthorize("@ss.hasPermi('pos:news:remove')" )
    @Log(title = "新闻" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iBizNewsService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
